{
  "nbformat": 4,
  "nbformat_minor": 0,
  "metadata": {
    "colab": {
      "name": "电影评论数据多分类.ipynb",
      "version": "0.3.2",
      "provenance": [],
      "collapsed_sections": []
    },
    "kernelspec": {
      "name": "python3",
      "display_name": "Python 3"
    },
    "accelerator": "GPU"
  },
  "cells": [
    {
      "metadata": {
        "id": "ChN8knF9GrLj",
        "colab_type": "code",
        "colab": {
          "base_uri": "https://localhost:8080/",
          "height": 89
        },
        "outputId": "90047866-68e2-4519-86a6-9708be088835"
      },
      "cell_type": "code",
      "source": [
        "# source : https://www.kaggle.com/nafisur/keras-models-lstm-cnn-gru-bidirectional-glove\n",
        "import numpy as np \n",
        "import pandas as pd \n",
        "import nltk\n",
        "import os\n",
        "import gc\n",
        "from keras.preprocessing import sequence,text\n",
        "from keras.preprocessing.text import Tokenizer\n",
        "from keras.models import Sequential\n",
        "from keras.layers import Dense,Dropout,Embedding,LSTM,Conv1D,GlobalMaxPooling1D,Flatten,MaxPooling1D,GRU,SpatialDropout1D,Bidirectional\n",
        "from keras.callbacks import EarlyStopping\n",
        "from keras.utils import to_categorical\n",
        "from keras.losses import categorical_crossentropy\n",
        "from keras.optimizers import Adam\n",
        "from sklearn.model_selection import train_test_split\n",
        "from sklearn.metrics import accuracy_score,confusion_matrix,classification_report,f1_score\n",
        "import matplotlib.pyplot as plt\n",
        "import warnings\n",
        "nltk.download('punkt')\n",
        "nltk.download('wordnet')\n",
        "warnings.filterwarnings(\"ignore\")\n",
        "#pd.set_option('display.max_colwidth',100)\n",
        "pd.set_option('display.max_colwidth', -1)"
      ],
      "execution_count": 0,
      "outputs": [
        {
          "output_type": "stream",
          "text": [
            "[nltk_data] Downloading package punkt to /root/nltk_data...\n",
            "[nltk_data]   Package punkt is already up-to-date!\n",
            "[nltk_data] Downloading package wordnet to /root/nltk_data...\n",
            "[nltk_data]   Package wordnet is already up-to-date!\n"
          ],
          "name": "stdout"
        }
      ]
    },
    {
      "metadata": {
        "id": "bhFcSGIDPXzN",
        "colab_type": "code",
        "colab": {
          "base_uri": "https://localhost:8080/",
          "height": 35
        },
        "outputId": "49122996-1caf-46a9-c7a1-2ed8f0ee3ae3"
      },
      "cell_type": "code",
      "source": [
        "from google.colab import drive\n",
        "drive.mount('/content/drive')"
      ],
      "execution_count": 0,
      "outputs": [
        {
          "output_type": "stream",
          "text": [
            "Mounted at /content/drive\n"
          ],
          "name": "stdout"
        }
      ]
    },
    {
      "metadata": {
        "id": "ZvhrFZUJGxVV",
        "colab_type": "code",
        "colab": {
          "base_uri": "https://localhost:8080/",
          "height": 35
        },
        "outputId": "6ccfaf43-8587-4be6-f0e8-a941dab102f3"
      },
      "cell_type": "code",
      "source": [
        "gc.collect()"
      ],
      "execution_count": 0,
      "outputs": [
        {
          "output_type": "execute_result",
          "data": {
            "text/plain": [
              "0"
            ]
          },
          "metadata": {
            "tags": []
          },
          "execution_count": 15
        }
      ]
    },
    {
      "metadata": {
        "id": "CDtGsH20HAoy",
        "colab_type": "code",
        "colab": {
          "base_uri": "https://localhost:8080/",
          "height": 239
        },
        "outputId": "5674bf60-3531-4d61-b1f8-5cf8aad2c250"
      },
      "cell_type": "code",
      "source": [
        "train = pd.read_csv('./drive/My Drive/data/moives/train.tsv',sep='\\t')\n",
        "print(train.shape)\n",
        "train.head()"
      ],
      "execution_count": 0,
      "outputs": [
        {
          "output_type": "stream",
          "text": [
            "(156060, 4)\n"
          ],
          "name": "stdout"
        },
        {
          "output_type": "execute_result",
          "data": {
            "text/html": [
              "<div>\n",
              "<style scoped>\n",
              "    .dataframe tbody tr th:only-of-type {\n",
              "        vertical-align: middle;\n",
              "    }\n",
              "\n",
              "    .dataframe tbody tr th {\n",
              "        vertical-align: top;\n",
              "    }\n",
              "\n",
              "    .dataframe thead th {\n",
              "        text-align: right;\n",
              "    }\n",
              "</style>\n",
              "<table border=\"1\" class=\"dataframe\">\n",
              "  <thead>\n",
              "    <tr style=\"text-align: right;\">\n",
              "      <th></th>\n",
              "      <th>PhraseId</th>\n",
              "      <th>SentenceId</th>\n",
              "      <th>Phrase</th>\n",
              "      <th>Sentiment</th>\n",
              "    </tr>\n",
              "  </thead>\n",
              "  <tbody>\n",
              "    <tr>\n",
              "      <th>0</th>\n",
              "      <td>1</td>\n",
              "      <td>1</td>\n",
              "      <td>A series of escapades demonstrating the adage that what is good for the goose is also good for the gander , some of which occasionally amuses but none of which amounts to much of a story .</td>\n",
              "      <td>1</td>\n",
              "    </tr>\n",
              "    <tr>\n",
              "      <th>1</th>\n",
              "      <td>2</td>\n",
              "      <td>1</td>\n",
              "      <td>A series of escapades demonstrating the adage that what is good for the goose</td>\n",
              "      <td>2</td>\n",
              "    </tr>\n",
              "    <tr>\n",
              "      <th>2</th>\n",
              "      <td>3</td>\n",
              "      <td>1</td>\n",
              "      <td>A series</td>\n",
              "      <td>2</td>\n",
              "    </tr>\n",
              "    <tr>\n",
              "      <th>3</th>\n",
              "      <td>4</td>\n",
              "      <td>1</td>\n",
              "      <td>A</td>\n",
              "      <td>2</td>\n",
              "    </tr>\n",
              "    <tr>\n",
              "      <th>4</th>\n",
              "      <td>5</td>\n",
              "      <td>1</td>\n",
              "      <td>series</td>\n",
              "      <td>2</td>\n",
              "    </tr>\n",
              "  </tbody>\n",
              "</table>\n",
              "</div>"
            ],
            "text/plain": [
              "   PhraseId  SentenceId  \\\n",
              "0  1         1            \n",
              "1  2         1            \n",
              "2  3         1            \n",
              "3  4         1            \n",
              "4  5         1            \n",
              "\n",
              "                                                                                                                                                                                         Phrase  \\\n",
              "0  A series of escapades demonstrating the adage that what is good for the goose is also good for the gander , some of which occasionally amuses but none of which amounts to much of a story .   \n",
              "1  A series of escapades demonstrating the adage that what is good for the goose                                                                                                                  \n",
              "2  A series                                                                                                                                                                                       \n",
              "3  A                                                                                                                                                                                              \n",
              "4  series                                                                                                                                                                                         \n",
              "\n",
              "   Sentiment  \n",
              "0  1          \n",
              "1  2          \n",
              "2  2          \n",
              "3  2          \n",
              "4  2          "
            ]
          },
          "metadata": {
            "tags": []
          },
          "execution_count": 16
        }
      ]
    },
    {
      "metadata": {
        "id": "vJBVYwSfH2v7",
        "colab_type": "code",
        "colab": {
          "base_uri": "https://localhost:8080/",
          "height": 222
        },
        "outputId": "5129a654-9a45-49ca-ad6d-4c93e3fd72e8"
      },
      "cell_type": "code",
      "source": [
        "test=pd.read_csv('./drive/My Drive/data/moives/test.tsv',sep='\\t')\n",
        "print(test.shape)\n",
        "test.head()"
      ],
      "execution_count": 0,
      "outputs": [
        {
          "output_type": "stream",
          "text": [
            "(66292, 3)\n"
          ],
          "name": "stdout"
        },
        {
          "output_type": "execute_result",
          "data": {
            "text/html": [
              "<div>\n",
              "<style scoped>\n",
              "    .dataframe tbody tr th:only-of-type {\n",
              "        vertical-align: middle;\n",
              "    }\n",
              "\n",
              "    .dataframe tbody tr th {\n",
              "        vertical-align: top;\n",
              "    }\n",
              "\n",
              "    .dataframe thead th {\n",
              "        text-align: right;\n",
              "    }\n",
              "</style>\n",
              "<table border=\"1\" class=\"dataframe\">\n",
              "  <thead>\n",
              "    <tr style=\"text-align: right;\">\n",
              "      <th></th>\n",
              "      <th>PhraseId</th>\n",
              "      <th>SentenceId</th>\n",
              "      <th>Phrase</th>\n",
              "    </tr>\n",
              "  </thead>\n",
              "  <tbody>\n",
              "    <tr>\n",
              "      <th>0</th>\n",
              "      <td>156061</td>\n",
              "      <td>8545</td>\n",
              "      <td>An intermittently pleasing but mostly routine effort .</td>\n",
              "    </tr>\n",
              "    <tr>\n",
              "      <th>1</th>\n",
              "      <td>156062</td>\n",
              "      <td>8545</td>\n",
              "      <td>An intermittently pleasing but mostly routine effort</td>\n",
              "    </tr>\n",
              "    <tr>\n",
              "      <th>2</th>\n",
              "      <td>156063</td>\n",
              "      <td>8545</td>\n",
              "      <td>An</td>\n",
              "    </tr>\n",
              "    <tr>\n",
              "      <th>3</th>\n",
              "      <td>156064</td>\n",
              "      <td>8545</td>\n",
              "      <td>intermittently pleasing but mostly routine effort</td>\n",
              "    </tr>\n",
              "    <tr>\n",
              "      <th>4</th>\n",
              "      <td>156065</td>\n",
              "      <td>8545</td>\n",
              "      <td>intermittently pleasing but mostly routine</td>\n",
              "    </tr>\n",
              "  </tbody>\n",
              "</table>\n",
              "</div>"
            ],
            "text/plain": [
              "   PhraseId  SentenceId  \\\n",
              "0  156061    8545         \n",
              "1  156062    8545         \n",
              "2  156063    8545         \n",
              "3  156064    8545         \n",
              "4  156065    8545         \n",
              "\n",
              "                                                   Phrase  \n",
              "0  An intermittently pleasing but mostly routine effort .  \n",
              "1  An intermittently pleasing but mostly routine effort    \n",
              "2  An                                                      \n",
              "3  intermittently pleasing but mostly routine effort       \n",
              "4  intermittently pleasing but mostly routine              "
            ]
          },
          "metadata": {
            "tags": []
          },
          "execution_count": 17
        }
      ]
    },
    {
      "metadata": {
        "id": "BcoAqkjctSR-",
        "colab_type": "code",
        "colab": {
          "base_uri": "https://localhost:8080/",
          "height": 204
        },
        "outputId": "4c907360-90be-42d1-dfb1-ef40b4515d57"
      },
      "cell_type": "code",
      "source": [
        "sub=pd.read_csv('./drive/My Drive/data/moives/sampleSubmission.csv')\n",
        "sub.head()"
      ],
      "execution_count": 0,
      "outputs": [
        {
          "output_type": "execute_result",
          "data": {
            "text/html": [
              "<div>\n",
              "<style scoped>\n",
              "    .dataframe tbody tr th:only-of-type {\n",
              "        vertical-align: middle;\n",
              "    }\n",
              "\n",
              "    .dataframe tbody tr th {\n",
              "        vertical-align: top;\n",
              "    }\n",
              "\n",
              "    .dataframe thead th {\n",
              "        text-align: right;\n",
              "    }\n",
              "</style>\n",
              "<table border=\"1\" class=\"dataframe\">\n",
              "  <thead>\n",
              "    <tr style=\"text-align: right;\">\n",
              "      <th></th>\n",
              "      <th>PhraseId</th>\n",
              "      <th>Sentiment</th>\n",
              "    </tr>\n",
              "  </thead>\n",
              "  <tbody>\n",
              "    <tr>\n",
              "      <th>0</th>\n",
              "      <td>156061</td>\n",
              "      <td>2</td>\n",
              "    </tr>\n",
              "    <tr>\n",
              "      <th>1</th>\n",
              "      <td>156062</td>\n",
              "      <td>2</td>\n",
              "    </tr>\n",
              "    <tr>\n",
              "      <th>2</th>\n",
              "      <td>156063</td>\n",
              "      <td>2</td>\n",
              "    </tr>\n",
              "    <tr>\n",
              "      <th>3</th>\n",
              "      <td>156064</td>\n",
              "      <td>2</td>\n",
              "    </tr>\n",
              "    <tr>\n",
              "      <th>4</th>\n",
              "      <td>156065</td>\n",
              "      <td>2</td>\n",
              "    </tr>\n",
              "  </tbody>\n",
              "</table>\n",
              "</div>"
            ],
            "text/plain": [
              "   PhraseId  Sentiment\n",
              "0  156061    2        \n",
              "1  156062    2        \n",
              "2  156063    2        \n",
              "3  156064    2        \n",
              "4  156065    2        "
            ]
          },
          "metadata": {
            "tags": []
          },
          "execution_count": 18
        }
      ]
    },
    {
      "metadata": {
        "id": "mrYjyg2Ptcuk",
        "colab_type": "code",
        "colab": {
          "base_uri": "https://localhost:8080/",
          "height": 204
        },
        "outputId": "9a607705-12af-4f98-c728-bd3cd480e493"
      },
      "cell_type": "code",
      "source": [
        "test['Sentiment']=-999\n",
        "test.head()"
      ],
      "execution_count": 0,
      "outputs": [
        {
          "output_type": "execute_result",
          "data": {
            "text/html": [
              "<div>\n",
              "<style scoped>\n",
              "    .dataframe tbody tr th:only-of-type {\n",
              "        vertical-align: middle;\n",
              "    }\n",
              "\n",
              "    .dataframe tbody tr th {\n",
              "        vertical-align: top;\n",
              "    }\n",
              "\n",
              "    .dataframe thead th {\n",
              "        text-align: right;\n",
              "    }\n",
              "</style>\n",
              "<table border=\"1\" class=\"dataframe\">\n",
              "  <thead>\n",
              "    <tr style=\"text-align: right;\">\n",
              "      <th></th>\n",
              "      <th>PhraseId</th>\n",
              "      <th>SentenceId</th>\n",
              "      <th>Phrase</th>\n",
              "      <th>Sentiment</th>\n",
              "    </tr>\n",
              "  </thead>\n",
              "  <tbody>\n",
              "    <tr>\n",
              "      <th>0</th>\n",
              "      <td>156061</td>\n",
              "      <td>8545</td>\n",
              "      <td>An intermittently pleasing but mostly routine effort .</td>\n",
              "      <td>-999</td>\n",
              "    </tr>\n",
              "    <tr>\n",
              "      <th>1</th>\n",
              "      <td>156062</td>\n",
              "      <td>8545</td>\n",
              "      <td>An intermittently pleasing but mostly routine effort</td>\n",
              "      <td>-999</td>\n",
              "    </tr>\n",
              "    <tr>\n",
              "      <th>2</th>\n",
              "      <td>156063</td>\n",
              "      <td>8545</td>\n",
              "      <td>An</td>\n",
              "      <td>-999</td>\n",
              "    </tr>\n",
              "    <tr>\n",
              "      <th>3</th>\n",
              "      <td>156064</td>\n",
              "      <td>8545</td>\n",
              "      <td>intermittently pleasing but mostly routine effort</td>\n",
              "      <td>-999</td>\n",
              "    </tr>\n",
              "    <tr>\n",
              "      <th>4</th>\n",
              "      <td>156065</td>\n",
              "      <td>8545</td>\n",
              "      <td>intermittently pleasing but mostly routine</td>\n",
              "      <td>-999</td>\n",
              "    </tr>\n",
              "  </tbody>\n",
              "</table>\n",
              "</div>"
            ],
            "text/plain": [
              "   PhraseId  SentenceId  \\\n",
              "0  156061    8545         \n",
              "1  156062    8545         \n",
              "2  156063    8545         \n",
              "3  156064    8545         \n",
              "4  156065    8545         \n",
              "\n",
              "                                                   Phrase  Sentiment  \n",
              "0  An intermittently pleasing but mostly routine effort . -999        \n",
              "1  An intermittently pleasing but mostly routine effort   -999        \n",
              "2  An                                                     -999        \n",
              "3  intermittently pleasing but mostly routine effort      -999        \n",
              "4  intermittently pleasing but mostly routine             -999        "
            ]
          },
          "metadata": {
            "tags": []
          },
          "execution_count": 19
        }
      ]
    },
    {
      "metadata": {
        "id": "SBSwDZ0KudX1",
        "colab_type": "code",
        "colab": {
          "base_uri": "https://localhost:8080/",
          "height": 222
        },
        "outputId": "6fc30e4f-a6d6-4497-a380-61f7ff35cd3b"
      },
      "cell_type": "code",
      "source": [
        "df=pd.concat([train,test],ignore_index=True)\n",
        "print(df.shape)\n",
        "df.tail()"
      ],
      "execution_count": 0,
      "outputs": [
        {
          "output_type": "stream",
          "text": [
            "(222352, 4)\n"
          ],
          "name": "stdout"
        },
        {
          "output_type": "execute_result",
          "data": {
            "text/html": [
              "<div>\n",
              "<style scoped>\n",
              "    .dataframe tbody tr th:only-of-type {\n",
              "        vertical-align: middle;\n",
              "    }\n",
              "\n",
              "    .dataframe tbody tr th {\n",
              "        vertical-align: top;\n",
              "    }\n",
              "\n",
              "    .dataframe thead th {\n",
              "        text-align: right;\n",
              "    }\n",
              "</style>\n",
              "<table border=\"1\" class=\"dataframe\">\n",
              "  <thead>\n",
              "    <tr style=\"text-align: right;\">\n",
              "      <th></th>\n",
              "      <th>PhraseId</th>\n",
              "      <th>SentenceId</th>\n",
              "      <th>Phrase</th>\n",
              "      <th>Sentiment</th>\n",
              "    </tr>\n",
              "  </thead>\n",
              "  <tbody>\n",
              "    <tr>\n",
              "      <th>222347</th>\n",
              "      <td>222348</td>\n",
              "      <td>11855</td>\n",
              "      <td>A long-winded , predictable scenario .</td>\n",
              "      <td>-999</td>\n",
              "    </tr>\n",
              "    <tr>\n",
              "      <th>222348</th>\n",
              "      <td>222349</td>\n",
              "      <td>11855</td>\n",
              "      <td>A long-winded , predictable scenario</td>\n",
              "      <td>-999</td>\n",
              "    </tr>\n",
              "    <tr>\n",
              "      <th>222349</th>\n",
              "      <td>222350</td>\n",
              "      <td>11855</td>\n",
              "      <td>A long-winded ,</td>\n",
              "      <td>-999</td>\n",
              "    </tr>\n",
              "    <tr>\n",
              "      <th>222350</th>\n",
              "      <td>222351</td>\n",
              "      <td>11855</td>\n",
              "      <td>A long-winded</td>\n",
              "      <td>-999</td>\n",
              "    </tr>\n",
              "    <tr>\n",
              "      <th>222351</th>\n",
              "      <td>222352</td>\n",
              "      <td>11855</td>\n",
              "      <td>predictable scenario</td>\n",
              "      <td>-999</td>\n",
              "    </tr>\n",
              "  </tbody>\n",
              "</table>\n",
              "</div>"
            ],
            "text/plain": [
              "        PhraseId  SentenceId                                  Phrase  \\\n",
              "222347  222348    11855       A long-winded , predictable scenario .   \n",
              "222348  222349    11855       A long-winded , predictable scenario     \n",
              "222349  222350    11855       A long-winded ,                          \n",
              "222350  222351    11855       A long-winded                            \n",
              "222351  222352    11855       predictable scenario                     \n",
              "\n",
              "        Sentiment  \n",
              "222347 -999        \n",
              "222348 -999        \n",
              "222349 -999        \n",
              "222350 -999        \n",
              "222351 -999        "
            ]
          },
          "metadata": {
            "tags": []
          },
          "execution_count": 20
        }
      ]
    },
    {
      "metadata": {
        "id": "2628MXjOuf3s",
        "colab_type": "code",
        "colab": {
          "base_uri": "https://localhost:8080/",
          "height": 35
        },
        "outputId": "568fbdaf-1884-4c92-c988-c857bc4ba272"
      },
      "cell_type": "code",
      "source": [
        "del train,test\n",
        "gc.collect()"
      ],
      "execution_count": 0,
      "outputs": [
        {
          "output_type": "execute_result",
          "data": {
            "text/plain": [
              "57"
            ]
          },
          "metadata": {
            "tags": []
          },
          "execution_count": 21
        }
      ]
    },
    {
      "metadata": {
        "id": "Z7ATCZkVui-0",
        "colab_type": "code",
        "colab": {}
      },
      "cell_type": "code",
      "source": [
        "from nltk.tokenize import word_tokenize\n",
        "from nltk import FreqDist\n",
        "from nltk.stem import SnowballStemmer,WordNetLemmatizer\n",
        "stemmer=SnowballStemmer('english')\n",
        "lemma=WordNetLemmatizer()\n",
        "from string import punctuation\n",
        "import re"
      ],
      "execution_count": 0,
      "outputs": []
    },
    {
      "metadata": {
        "id": "k1M10SILNq6q",
        "colab_type": "code",
        "colab": {}
      },
      "cell_type": "code",
      "source": [
        "#数据清洗,大写转小写,删除标点符号\n",
        "def clean_review(review_col):\n",
        "    review_corpus=[]\n",
        "    for i in range(0,len(review_col)):\n",
        "        review=str(review_col[i])\n",
        "        review=re.sub('[^a-zA-Z]',' ',review)\n",
        "        #review=[stemmer.stem(w) for w in word_tokenize(str(review).lower())]\n",
        "        review=[lemma.lemmatize(w) for w in word_tokenize(str(review).lower())]\n",
        "        review=' '.join(review)\n",
        "        review_corpus.append(review)\n",
        "    return review_corpus"
      ],
      "execution_count": 0,
      "outputs": []
    },
    {
      "metadata": {
        "id": "mqM3aNUBNtTi",
        "colab_type": "code",
        "colab": {
          "base_uri": "https://localhost:8080/",
          "height": 272
        },
        "outputId": "90dfd672-11a6-4dbc-84dd-9393d59b0a7b"
      },
      "cell_type": "code",
      "source": [
        "df['clean_review']=clean_review(df.Phrase.values)\n",
        "df.head()"
      ],
      "execution_count": 0,
      "outputs": [
        {
          "output_type": "execute_result",
          "data": {
            "text/html": [
              "<div>\n",
              "<style scoped>\n",
              "    .dataframe tbody tr th:only-of-type {\n",
              "        vertical-align: middle;\n",
              "    }\n",
              "\n",
              "    .dataframe tbody tr th {\n",
              "        vertical-align: top;\n",
              "    }\n",
              "\n",
              "    .dataframe thead th {\n",
              "        text-align: right;\n",
              "    }\n",
              "</style>\n",
              "<table border=\"1\" class=\"dataframe\">\n",
              "  <thead>\n",
              "    <tr style=\"text-align: right;\">\n",
              "      <th></th>\n",
              "      <th>PhraseId</th>\n",
              "      <th>SentenceId</th>\n",
              "      <th>Phrase</th>\n",
              "      <th>Sentiment</th>\n",
              "      <th>clean_review</th>\n",
              "    </tr>\n",
              "  </thead>\n",
              "  <tbody>\n",
              "    <tr>\n",
              "      <th>0</th>\n",
              "      <td>1</td>\n",
              "      <td>1</td>\n",
              "      <td>A series of escapades demonstrating the adage that what is good for the goose is also good for the gander , some of which occasionally amuses but none of which amounts to much of a story .</td>\n",
              "      <td>1</td>\n",
              "      <td>a series of escapade demonstrating the adage that what is good for the goose is also good for the gander some of which occasionally amuses but none of which amount to much of a story</td>\n",
              "    </tr>\n",
              "    <tr>\n",
              "      <th>1</th>\n",
              "      <td>2</td>\n",
              "      <td>1</td>\n",
              "      <td>A series of escapades demonstrating the adage that what is good for the goose</td>\n",
              "      <td>2</td>\n",
              "      <td>a series of escapade demonstrating the adage that what is good for the goose</td>\n",
              "    </tr>\n",
              "    <tr>\n",
              "      <th>2</th>\n",
              "      <td>3</td>\n",
              "      <td>1</td>\n",
              "      <td>A series</td>\n",
              "      <td>2</td>\n",
              "      <td>a series</td>\n",
              "    </tr>\n",
              "    <tr>\n",
              "      <th>3</th>\n",
              "      <td>4</td>\n",
              "      <td>1</td>\n",
              "      <td>A</td>\n",
              "      <td>2</td>\n",
              "      <td>a</td>\n",
              "    </tr>\n",
              "    <tr>\n",
              "      <th>4</th>\n",
              "      <td>5</td>\n",
              "      <td>1</td>\n",
              "      <td>series</td>\n",
              "      <td>2</td>\n",
              "      <td>series</td>\n",
              "    </tr>\n",
              "  </tbody>\n",
              "</table>\n",
              "</div>"
            ],
            "text/plain": [
              "   PhraseId  SentenceId  \\\n",
              "0  1         1            \n",
              "1  2         1            \n",
              "2  3         1            \n",
              "3  4         1            \n",
              "4  5         1            \n",
              "\n",
              "                                                                                                                                                                                         Phrase  \\\n",
              "0  A series of escapades demonstrating the adage that what is good for the goose is also good for the gander , some of which occasionally amuses but none of which amounts to much of a story .   \n",
              "1  A series of escapades demonstrating the adage that what is good for the goose                                                                                                                  \n",
              "2  A series                                                                                                                                                                                       \n",
              "3  A                                                                                                                                                                                              \n",
              "4  series                                                                                                                                                                                         \n",
              "\n",
              "   Sentiment  \\\n",
              "0  1           \n",
              "1  2           \n",
              "2  2           \n",
              "3  2           \n",
              "4  2           \n",
              "\n",
              "                                                                                                                                                                             clean_review  \n",
              "0  a series of escapade demonstrating the adage that what is good for the goose is also good for the gander some of which occasionally amuses but none of which amount to much of a story  \n",
              "1  a series of escapade demonstrating the adage that what is good for the goose                                                                                                            \n",
              "2  a series                                                                                                                                                                                \n",
              "3  a                                                                                                                                                                                       \n",
              "4  series                                                                                                                                                                                  "
            ]
          },
          "metadata": {
            "tags": []
          },
          "execution_count": 24
        }
      ]
    },
    {
      "metadata": {
        "id": "nKiwMMLlT-BE",
        "colab_type": "code",
        "colab": {
          "base_uri": "https://localhost:8080/",
          "height": 35
        },
        "outputId": "36dff122-eb07-43df-ea21-416413ac50b3"
      },
      "cell_type": "code",
      "source": [
        "df_train=df[df.Sentiment!=-999]\n",
        "df_train.shape"
      ],
      "execution_count": 0,
      "outputs": [
        {
          "output_type": "execute_result",
          "data": {
            "text/plain": [
              "(156060, 5)"
            ]
          },
          "metadata": {
            "tags": []
          },
          "execution_count": 25
        }
      ]
    },
    {
      "metadata": {
        "id": "s_9jbFoyUHh7",
        "colab_type": "code",
        "colab": {
          "base_uri": "https://localhost:8080/",
          "height": 222
        },
        "outputId": "1b3f6b07-c7f2-48cd-fe29-253eba51bbf6"
      },
      "cell_type": "code",
      "source": [
        "df_test=df[df.Sentiment==-999]\n",
        "df_test.drop('Sentiment',axis=1,inplace=True)\n",
        "print(df_test.shape)\n",
        "df_test.head()"
      ],
      "execution_count": 0,
      "outputs": [
        {
          "output_type": "stream",
          "text": [
            "(66292, 4)\n"
          ],
          "name": "stdout"
        },
        {
          "output_type": "execute_result",
          "data": {
            "text/html": [
              "<div>\n",
              "<style scoped>\n",
              "    .dataframe tbody tr th:only-of-type {\n",
              "        vertical-align: middle;\n",
              "    }\n",
              "\n",
              "    .dataframe tbody tr th {\n",
              "        vertical-align: top;\n",
              "    }\n",
              "\n",
              "    .dataframe thead th {\n",
              "        text-align: right;\n",
              "    }\n",
              "</style>\n",
              "<table border=\"1\" class=\"dataframe\">\n",
              "  <thead>\n",
              "    <tr style=\"text-align: right;\">\n",
              "      <th></th>\n",
              "      <th>PhraseId</th>\n",
              "      <th>SentenceId</th>\n",
              "      <th>Phrase</th>\n",
              "      <th>clean_review</th>\n",
              "    </tr>\n",
              "  </thead>\n",
              "  <tbody>\n",
              "    <tr>\n",
              "      <th>156060</th>\n",
              "      <td>156061</td>\n",
              "      <td>8545</td>\n",
              "      <td>An intermittently pleasing but mostly routine effort .</td>\n",
              "      <td>an intermittently pleasing but mostly routine effort</td>\n",
              "    </tr>\n",
              "    <tr>\n",
              "      <th>156061</th>\n",
              "      <td>156062</td>\n",
              "      <td>8545</td>\n",
              "      <td>An intermittently pleasing but mostly routine effort</td>\n",
              "      <td>an intermittently pleasing but mostly routine effort</td>\n",
              "    </tr>\n",
              "    <tr>\n",
              "      <th>156062</th>\n",
              "      <td>156063</td>\n",
              "      <td>8545</td>\n",
              "      <td>An</td>\n",
              "      <td>an</td>\n",
              "    </tr>\n",
              "    <tr>\n",
              "      <th>156063</th>\n",
              "      <td>156064</td>\n",
              "      <td>8545</td>\n",
              "      <td>intermittently pleasing but mostly routine effort</td>\n",
              "      <td>intermittently pleasing but mostly routine effort</td>\n",
              "    </tr>\n",
              "    <tr>\n",
              "      <th>156064</th>\n",
              "      <td>156065</td>\n",
              "      <td>8545</td>\n",
              "      <td>intermittently pleasing but mostly routine</td>\n",
              "      <td>intermittently pleasing but mostly routine</td>\n",
              "    </tr>\n",
              "  </tbody>\n",
              "</table>\n",
              "</div>"
            ],
            "text/plain": [
              "        PhraseId  SentenceId  \\\n",
              "156060  156061    8545         \n",
              "156061  156062    8545         \n",
              "156062  156063    8545         \n",
              "156063  156064    8545         \n",
              "156064  156065    8545         \n",
              "\n",
              "                                                        Phrase  \\\n",
              "156060  An intermittently pleasing but mostly routine effort .   \n",
              "156061  An intermittently pleasing but mostly routine effort     \n",
              "156062  An                                                       \n",
              "156063  intermittently pleasing but mostly routine effort        \n",
              "156064  intermittently pleasing but mostly routine               \n",
              "\n",
              "                                                clean_review  \n",
              "156060  an intermittently pleasing but mostly routine effort  \n",
              "156061  an intermittently pleasing but mostly routine effort  \n",
              "156062  an                                                    \n",
              "156063  intermittently pleasing but mostly routine effort     \n",
              "156064  intermittently pleasing but mostly routine            "
            ]
          },
          "metadata": {
            "tags": []
          },
          "execution_count": 26
        }
      ]
    },
    {
      "metadata": {
        "id": "M3MEtz3KWCV0",
        "colab_type": "code",
        "colab": {
          "base_uri": "https://localhost:8080/",
          "height": 35
        },
        "outputId": "0ac0d702-5ba8-4f26-8d64-564e34b73714"
      },
      "cell_type": "code",
      "source": [
        "del df\n",
        "gc.collect()"
      ],
      "execution_count": 0,
      "outputs": [
        {
          "output_type": "execute_result",
          "data": {
            "text/plain": [
              "23"
            ]
          },
          "metadata": {
            "tags": []
          },
          "execution_count": 27
        }
      ]
    },
    {
      "metadata": {
        "id": "WJ3_XeLUWPj7",
        "colab_type": "code",
        "colab": {
          "base_uri": "https://localhost:8080/",
          "height": 35
        },
        "outputId": "a8291d58-b5b6-40bd-ee33-ab951d9de139"
      },
      "cell_type": "code",
      "source": [
        "train_text=df_train.clean_review.values\n",
        "test_text=df_test.clean_review.values\n",
        "target=df_train.Sentiment.values\n",
        "#分类数据转one hot\n",
        "y=to_categorical(target)\n",
        "\n",
        "print(train_text.shape,target.shape,y.shape)"
      ],
      "execution_count": 0,
      "outputs": [
        {
          "output_type": "stream",
          "text": [
            "(156060,) (156060,) (156060, 5)\n"
          ],
          "name": "stdout"
        }
      ]
    },
    {
      "metadata": {
        "id": "-PWeWoecYUll",
        "colab_type": "code",
        "colab": {
          "base_uri": "https://localhost:8080/",
          "height": 53
        },
        "outputId": "c8df5602-7968-4177-9b2e-b5d7a0fe814e"
      },
      "cell_type": "code",
      "source": [
        "X_train_text,X_val_text,y_train,y_val=train_test_split(train_text,y,test_size=0.2,stratify=y,random_state=123)\n",
        "print(X_train_text.shape,y_train.shape)\n",
        "print(X_val_text.shape,y_val.shape)"
      ],
      "execution_count": 0,
      "outputs": [
        {
          "output_type": "stream",
          "text": [
            "(124848,) (124848, 5)\n",
            "(31212,) (31212, 5)\n"
          ],
          "name": "stdout"
        }
      ]
    },
    {
      "metadata": {
        "id": "4tl29l07YuCz",
        "colab_type": "code",
        "colab": {
          "base_uri": "https://localhost:8080/",
          "height": 35
        },
        "outputId": "92e97e03-f7a9-4d01-a210-070fb1b8f1b6"
      },
      "cell_type": "code",
      "source": [
        "#Finding number of unique words in train set\n",
        "\n",
        "all_words=' '.join(X_train_text) # 将array中的所有字符串连接成一个字符串\n",
        "all_words=word_tokenize(all_words)#对单词进行分割\n",
        "dist=FreqDist(all_words) #计算词频wordcount\n",
        "num_unique_word=len(dist)\n",
        "num_unique_word"
      ],
      "execution_count": 0,
      "outputs": [
        {
          "output_type": "execute_result",
          "data": {
            "text/plain": [
              "13732"
            ]
          },
          "metadata": {
            "tags": []
          },
          "execution_count": 30
        }
      ]
    },
    {
      "metadata": {
        "id": "Ht4CZXxbZLG2",
        "colab_type": "code",
        "colab": {
          "base_uri": "https://localhost:8080/",
          "height": 35
        },
        "outputId": "db9ed0cf-45ae-498a-d902-6651be06f8b9"
      },
      "cell_type": "code",
      "source": [
        "#Finding max length of a review in train set\n",
        "r_len=[]\n",
        "for text in X_train_text:\n",
        "    word=word_tokenize(text)\n",
        "    l=len(word)\n",
        "    r_len.append(l)\n",
        "    \n",
        "MAX_REVIEW_LEN=np.max(r_len)\n",
        "MAX_REVIEW_LEN"
      ],
      "execution_count": 0,
      "outputs": [
        {
          "output_type": "execute_result",
          "data": {
            "text/plain": [
              "48"
            ]
          },
          "metadata": {
            "tags": []
          },
          "execution_count": 31
        }
      ]
    },
    {
      "metadata": {
        "id": "_sMdZ_xgjEdY",
        "colab_type": "text"
      },
      "cell_type": "markdown",
      "source": [
        "##Building Keras LSTM model##"
      ]
    },
    {
      "metadata": {
        "id": "yhaqTppTaScp",
        "colab_type": "code",
        "colab": {}
      },
      "cell_type": "code",
      "source": [
        "max_features = num_unique_word\n",
        "max_words = MAX_REVIEW_LEN\n",
        "batch_size = 128\n",
        "epochs = 3\n",
        "num_classes=5"
      ],
      "execution_count": 0,
      "outputs": []
    },
    {
      "metadata": {
        "id": "58OAtx07enW2",
        "colab_type": "code",
        "colab": {}
      },
      "cell_type": "code",
      "source": [
        "#Tokenize Text\n",
        "tokenizer = Tokenizer(num_words=max_features)\n",
        "tokenizer.fit_on_texts(list(X_train_text))\n",
        "X_train = tokenizer.texts_to_sequences(X_train_text)\n",
        "X_val = tokenizer.texts_to_sequences(X_val_text)\n",
        "X_test = tokenizer.texts_to_sequences(test_text)"
      ],
      "execution_count": 0,
      "outputs": []
    },
    {
      "metadata": {
        "id": "zZXVhfAkfFqG",
        "colab_type": "code",
        "colab": {
          "base_uri": "https://localhost:8080/",
          "height": 35
        },
        "outputId": "a0fe8ce5-de2e-4b53-ae70-d3b5cda57aba"
      },
      "cell_type": "code",
      "source": [
        "#sequence padding\n",
        "X_train = sequence.pad_sequences(X_train, maxlen=max_words)\n",
        "X_val = sequence.pad_sequences(X_val, maxlen=max_words)\n",
        "X_test = sequence.pad_sequences(X_test, maxlen=max_words)\n",
        "print(X_train.shape,X_val.shape,X_test.shape)"
      ],
      "execution_count": 0,
      "outputs": [
        {
          "output_type": "stream",
          "text": [
            "(124848, 48) (31212, 48) (66292, 48)\n"
          ],
          "name": "stdout"
        }
      ]
    },
    {
      "metadata": {
        "id": "y6Ur5VcqkBub",
        "colab_type": "text"
      },
      "cell_type": "markdown",
      "source": [
        "##1.LSTM model##"
      ]
    },
    {
      "metadata": {
        "id": "n97YpLJcj8LR",
        "colab_type": "code",
        "colab": {
          "base_uri": "https://localhost:8080/",
          "height": 287
        },
        "outputId": "897e5daf-9b9f-497a-f760-84809b4a04d9"
      },
      "cell_type": "code",
      "source": [
        "model1=Sequential()\n",
        "model1.add(Embedding(max_features,100,mask_zero=True))\n",
        "model1.add(LSTM(64,dropout=0.4, recurrent_dropout=0.4,return_sequences=True))\n",
        "model1.add(LSTM(32,dropout=0.5, recurrent_dropout=0.5,return_sequences=False))\n",
        "model1.add(Dense(num_classes,activation='softmax'))\n",
        "model1.compile(loss='categorical_crossentropy',optimizer=Adam(lr=0.001),metrics=['accuracy'])\n",
        "model1.summary()"
      ],
      "execution_count": 0,
      "outputs": [
        {
          "output_type": "stream",
          "text": [
            "_________________________________________________________________\n",
            "Layer (type)                 Output Shape              Param #   \n",
            "=================================================================\n",
            "embedding_1 (Embedding)      (None, None, 100)         1373200   \n",
            "_________________________________________________________________\n",
            "lstm_1 (LSTM)                (None, None, 64)          42240     \n",
            "_________________________________________________________________\n",
            "lstm_2 (LSTM)                (None, 32)                12416     \n",
            "_________________________________________________________________\n",
            "dense_1 (Dense)              (None, 5)                 165       \n",
            "=================================================================\n",
            "Total params: 1,428,021\n",
            "Trainable params: 1,428,021\n",
            "Non-trainable params: 0\n",
            "_________________________________________________________________\n"
          ],
          "name": "stdout"
        }
      ]
    },
    {
      "metadata": {
        "id": "6baG9i89kRWg",
        "colab_type": "code",
        "colab": {
          "base_uri": "https://localhost:8080/",
          "height": 179
        },
        "outputId": "825b1793-93b5-4db3-d003-672853ec8662"
      },
      "cell_type": "code",
      "source": [
        "%%time\n",
        "history1=model1.fit(X_train, y_train, validation_data=(X_val, y_val),epochs=epochs, batch_size=batch_size, verbose=1)"
      ],
      "execution_count": 0,
      "outputs": [
        {
          "output_type": "stream",
          "text": [
            "Train on 124848 samples, validate on 31212 samples\n",
            "Epoch 1/3\n",
            "124848/124848 [==============================] - 351s 3ms/step - loss: 1.0755 - acc: 0.5782 - val_loss: 0.8735 - val_acc: 0.6457\n",
            "Epoch 2/3\n",
            "124848/124848 [==============================] - 349s 3ms/step - loss: 0.8335 - acc: 0.6573 - val_loss: 0.8306 - val_acc: 0.6621\n",
            "Epoch 3/3\n",
            "124848/124848 [==============================] - 348s 3ms/step - loss: 0.7727 - acc: 0.6808 - val_loss: 0.8180 - val_acc: 0.6643\n",
            "CPU times: user 22min 11s, sys: 2min 23s, total: 24min 35s\n",
            "Wall time: 17min 30s\n"
          ],
          "name": "stdout"
        }
      ]
    },
    {
      "metadata": {
        "id": "QGYjKu2Oka5w",
        "colab_type": "code",
        "colab": {
          "base_uri": "https://localhost:8080/",
          "height": 35
        },
        "outputId": "0283095f-f5dc-442e-ef6b-2a26f730e271"
      },
      "cell_type": "code",
      "source": [
        "y_pred1=model1.predict_classes(X_test,verbose=1)"
      ],
      "execution_count": 0,
      "outputs": [
        {
          "output_type": "stream",
          "text": [
            "66292/66292 [==============================] - 212s 3ms/step\n"
          ],
          "name": "stdout"
        }
      ]
    },
    {
      "metadata": {
        "id": "Hbk1rtvOozQU",
        "colab_type": "code",
        "colab": {
          "base_uri": "https://localhost:8080/",
          "height": 204
        },
        "outputId": "2afaa617-f057-4f31-8392-6adf96e5f66a"
      },
      "cell_type": "code",
      "source": [
        "sub.Sentiment=y_pred1\n",
        "sub.to_csv('./drive/My Drive/data/moives/sub1_LSTM.csv',index=False)\n",
        "sub.head()"
      ],
      "execution_count": 0,
      "outputs": [
        {
          "output_type": "execute_result",
          "data": {
            "text/html": [
              "<div>\n",
              "<style scoped>\n",
              "    .dataframe tbody tr th:only-of-type {\n",
              "        vertical-align: middle;\n",
              "    }\n",
              "\n",
              "    .dataframe tbody tr th {\n",
              "        vertical-align: top;\n",
              "    }\n",
              "\n",
              "    .dataframe thead th {\n",
              "        text-align: right;\n",
              "    }\n",
              "</style>\n",
              "<table border=\"1\" class=\"dataframe\">\n",
              "  <thead>\n",
              "    <tr style=\"text-align: right;\">\n",
              "      <th></th>\n",
              "      <th>PhraseId</th>\n",
              "      <th>Sentiment</th>\n",
              "    </tr>\n",
              "  </thead>\n",
              "  <tbody>\n",
              "    <tr>\n",
              "      <th>0</th>\n",
              "      <td>156061</td>\n",
              "      <td>3</td>\n",
              "    </tr>\n",
              "    <tr>\n",
              "      <th>1</th>\n",
              "      <td>156062</td>\n",
              "      <td>3</td>\n",
              "    </tr>\n",
              "    <tr>\n",
              "      <th>2</th>\n",
              "      <td>156063</td>\n",
              "      <td>2</td>\n",
              "    </tr>\n",
              "    <tr>\n",
              "      <th>3</th>\n",
              "      <td>156064</td>\n",
              "      <td>3</td>\n",
              "    </tr>\n",
              "    <tr>\n",
              "      <th>4</th>\n",
              "      <td>156065</td>\n",
              "      <td>2</td>\n",
              "    </tr>\n",
              "  </tbody>\n",
              "</table>\n",
              "</div>"
            ],
            "text/plain": [
              "   PhraseId  Sentiment\n",
              "0  156061    3        \n",
              "1  156062    3        \n",
              "2  156063    2        \n",
              "3  156064    3        \n",
              "4  156065    2        "
            ]
          },
          "metadata": {
            "tags": []
          },
          "execution_count": 38
        }
      ]
    },
    {
      "metadata": {
        "id": "BMdDwwPeqHp9",
        "colab_type": "text"
      },
      "cell_type": "markdown",
      "source": [
        "##2. CNN##"
      ]
    },
    {
      "metadata": {
        "id": "CzXWIwfMqQFq",
        "colab_type": "code",
        "colab": {
          "base_uri": "https://localhost:8080/",
          "height": 395
        },
        "outputId": "b2b35d29-83ee-42d4-f301-b7d879eb19e9"
      },
      "cell_type": "code",
      "source": [
        "model2= Sequential()\n",
        "model2.add(Embedding(max_features,100,input_length=max_words))\n",
        "model2.add(Dropout(0.2))\n",
        "\n",
        "model2.add(Conv1D(64,kernel_size=3,padding='same',activation='relu',strides=1))\n",
        "model2.add(GlobalMaxPooling1D())\n",
        "\n",
        "model2.add(Dense(128,activation='relu'))\n",
        "model2.add(Dropout(0.2))\n",
        "\n",
        "model2.add(Dense(num_classes,activation='softmax'))\n",
        "\n",
        "model2.compile(loss='categorical_crossentropy',optimizer='adam',metrics=['accuracy'])\n",
        "\n",
        "model2.summary()"
      ],
      "execution_count": 0,
      "outputs": [
        {
          "output_type": "stream",
          "text": [
            "_________________________________________________________________\n",
            "Layer (type)                 Output Shape              Param #   \n",
            "=================================================================\n",
            "embedding_2 (Embedding)      (None, 48, 100)           1373200   \n",
            "_________________________________________________________________\n",
            "dropout_1 (Dropout)          (None, 48, 100)           0         \n",
            "_________________________________________________________________\n",
            "conv1d_1 (Conv1D)            (None, 48, 64)            19264     \n",
            "_________________________________________________________________\n",
            "global_max_pooling1d_1 (Glob (None, 64)                0         \n",
            "_________________________________________________________________\n",
            "dense_2 (Dense)              (None, 128)               8320      \n",
            "_________________________________________________________________\n",
            "dropout_2 (Dropout)          (None, 128)               0         \n",
            "_________________________________________________________________\n",
            "dense_3 (Dense)              (None, 5)                 645       \n",
            "=================================================================\n",
            "Total params: 1,401,429\n",
            "Trainable params: 1,401,429\n",
            "Non-trainable params: 0\n",
            "_________________________________________________________________\n"
          ],
          "name": "stdout"
        }
      ]
    },
    {
      "metadata": {
        "id": "Bz-6ICItqe4A",
        "colab_type": "code",
        "colab": {
          "base_uri": "https://localhost:8080/",
          "height": 179
        },
        "outputId": "8e03e478-71d3-4163-cd92-03dec57e0a45"
      },
      "cell_type": "code",
      "source": [
        "%%time\n",
        "history2=model2.fit(X_train, y_train, validation_data=(X_val, y_val),epochs=epochs, batch_size=batch_size, verbose=1)"
      ],
      "execution_count": 0,
      "outputs": [
        {
          "output_type": "stream",
          "text": [
            "Train on 124848 samples, validate on 31212 samples\n",
            "Epoch 1/3\n",
            "124848/124848 [==============================] - 13s 102us/step - loss: 0.9970 - acc: 0.6023 - val_loss: 0.8480 - val_acc: 0.6527\n",
            "Epoch 2/3\n",
            "124848/124848 [==============================] - 11s 90us/step - loss: 0.7847 - acc: 0.6772 - val_loss: 0.8190 - val_acc: 0.6626\n",
            "Epoch 3/3\n",
            "124848/124848 [==============================] - 11s 90us/step - loss: 0.7047 - acc: 0.7061 - val_loss: 0.8051 - val_acc: 0.6644\n",
            "CPU times: user 37.2 s, sys: 6.94 s, total: 44.1 s\n",
            "Wall time: 35.6 s\n"
          ],
          "name": "stdout"
        }
      ]
    },
    {
      "metadata": {
        "id": "fy_PmUEvq7Ix",
        "colab_type": "code",
        "colab": {
          "base_uri": "https://localhost:8080/",
          "height": 35
        },
        "outputId": "41ef15df-57a3-46b7-d953-4e2b39432871"
      },
      "cell_type": "code",
      "source": [
        "y_pred2=model2.predict_classes(X_test, verbose=1)"
      ],
      "execution_count": 0,
      "outputs": [
        {
          "output_type": "stream",
          "text": [
            "66292/66292 [==============================] - 3s 52us/step\n"
          ],
          "name": "stdout"
        }
      ]
    },
    {
      "metadata": {
        "id": "soQmy_QZslbR",
        "colab_type": "code",
        "colab": {
          "base_uri": "https://localhost:8080/",
          "height": 204
        },
        "outputId": "f3de3417-a242-408a-ba35-821709508ca4"
      },
      "cell_type": "code",
      "source": [
        "sub.Sentiment=y_pred2\n",
        "sub.to_csv('./drive/My Drive/data/moives/sub2_CNN.csv',index=False)\n",
        "sub.head()"
      ],
      "execution_count": 0,
      "outputs": [
        {
          "output_type": "execute_result",
          "data": {
            "text/html": [
              "<div>\n",
              "<style scoped>\n",
              "    .dataframe tbody tr th:only-of-type {\n",
              "        vertical-align: middle;\n",
              "    }\n",
              "\n",
              "    .dataframe tbody tr th {\n",
              "        vertical-align: top;\n",
              "    }\n",
              "\n",
              "    .dataframe thead th {\n",
              "        text-align: right;\n",
              "    }\n",
              "</style>\n",
              "<table border=\"1\" class=\"dataframe\">\n",
              "  <thead>\n",
              "    <tr style=\"text-align: right;\">\n",
              "      <th></th>\n",
              "      <th>PhraseId</th>\n",
              "      <th>Sentiment</th>\n",
              "    </tr>\n",
              "  </thead>\n",
              "  <tbody>\n",
              "    <tr>\n",
              "      <th>0</th>\n",
              "      <td>156061</td>\n",
              "      <td>2</td>\n",
              "    </tr>\n",
              "    <tr>\n",
              "      <th>1</th>\n",
              "      <td>156062</td>\n",
              "      <td>2</td>\n",
              "    </tr>\n",
              "    <tr>\n",
              "      <th>2</th>\n",
              "      <td>156063</td>\n",
              "      <td>2</td>\n",
              "    </tr>\n",
              "    <tr>\n",
              "      <th>3</th>\n",
              "      <td>156064</td>\n",
              "      <td>2</td>\n",
              "    </tr>\n",
              "    <tr>\n",
              "      <th>4</th>\n",
              "      <td>156065</td>\n",
              "      <td>2</td>\n",
              "    </tr>\n",
              "  </tbody>\n",
              "</table>\n",
              "</div>"
            ],
            "text/plain": [
              "   PhraseId  Sentiment\n",
              "0  156061    2        \n",
              "1  156062    2        \n",
              "2  156063    2        \n",
              "3  156064    2        \n",
              "4  156065    2        "
            ]
          },
          "metadata": {
            "tags": []
          },
          "execution_count": 42
        }
      ]
    },
    {
      "metadata": {
        "id": "RjibO9pDtOTe",
        "colab_type": "text"
      },
      "cell_type": "markdown",
      "source": [
        "##3. CNN +GRU##"
      ]
    },
    {
      "metadata": {
        "id": "GRjXwlDSs2QI",
        "colab_type": "code",
        "colab": {
          "base_uri": "https://localhost:8080/",
          "height": 503
        },
        "outputId": "0a14be3b-7bbd-4665-a07a-5ba1b1a95cbb"
      },
      "cell_type": "code",
      "source": [
        "model3= Sequential()\n",
        "model3.add(Embedding(max_features,100,input_length=max_words))\n",
        "model3.add(Conv1D(64,kernel_size=3,padding='same',activation='relu'))\n",
        "model3.add(MaxPooling1D(pool_size=2))\n",
        "model3.add(Dropout(0.25))\n",
        "model3.add(GRU(128,return_sequences=True))\n",
        "model3.add(Dropout(0.3))\n",
        "model3.add(Flatten())\n",
        "model3.add(Dense(128,activation='relu'))\n",
        "model3.add(Dropout(0.5))\n",
        "model3.add(Dense(5,activation='softmax'))\n",
        "model3.compile(loss='categorical_crossentropy',optimizer=Adam(lr=0.001),metrics=['accuracy'])\n",
        "model3.summary()"
      ],
      "execution_count": 0,
      "outputs": [
        {
          "output_type": "stream",
          "text": [
            "_________________________________________________________________\n",
            "Layer (type)                 Output Shape              Param #   \n",
            "=================================================================\n",
            "embedding_3 (Embedding)      (None, 48, 100)           1373200   \n",
            "_________________________________________________________________\n",
            "conv1d_2 (Conv1D)            (None, 48, 64)            19264     \n",
            "_________________________________________________________________\n",
            "max_pooling1d_1 (MaxPooling1 (None, 24, 64)            0         \n",
            "_________________________________________________________________\n",
            "dropout_3 (Dropout)          (None, 24, 64)            0         \n",
            "_________________________________________________________________\n",
            "gru_1 (GRU)                  (None, 24, 128)           74112     \n",
            "_________________________________________________________________\n",
            "dropout_4 (Dropout)          (None, 24, 128)           0         \n",
            "_________________________________________________________________\n",
            "flatten_1 (Flatten)          (None, 3072)              0         \n",
            "_________________________________________________________________\n",
            "dense_4 (Dense)              (None, 128)               393344    \n",
            "_________________________________________________________________\n",
            "dropout_5 (Dropout)          (None, 128)               0         \n",
            "_________________________________________________________________\n",
            "dense_5 (Dense)              (None, 5)                 645       \n",
            "=================================================================\n",
            "Total params: 1,860,565\n",
            "Trainable params: 1,860,565\n",
            "Non-trainable params: 0\n",
            "_________________________________________________________________\n"
          ],
          "name": "stdout"
        }
      ]
    },
    {
      "metadata": {
        "id": "hB-2o21PtVKx",
        "colab_type": "code",
        "colab": {
          "base_uri": "https://localhost:8080/",
          "height": 179
        },
        "outputId": "e0235f9d-e94d-4143-9d81-e67036a97616"
      },
      "cell_type": "code",
      "source": [
        "%%time\n",
        "history3=model3.fit(X_train, y_train, validation_data=(X_val, y_val),epochs=epochs, batch_size=batch_size, verbose=1)"
      ],
      "execution_count": 0,
      "outputs": [
        {
          "output_type": "stream",
          "text": [
            "Train on 124848 samples, validate on 31212 samples\n",
            "Epoch 1/3\n",
            "124848/124848 [==============================] - 66s 529us/step - loss: 1.0265 - acc: 0.5910 - val_loss: 0.8593 - val_acc: 0.6499\n",
            "Epoch 2/3\n",
            "124848/124848 [==============================] - 65s 523us/step - loss: 0.8064 - acc: 0.6694 - val_loss: 0.8172 - val_acc: 0.6606\n",
            "Epoch 3/3\n",
            "124848/124848 [==============================] - 64s 516us/step - loss: 0.7266 - acc: 0.6968 - val_loss: 0.8091 - val_acc: 0.6682\n",
            "CPU times: user 3min 32s, sys: 23 s, total: 3min 55s\n",
            "Wall time: 3min 16s\n"
          ],
          "name": "stdout"
        }
      ]
    },
    {
      "metadata": {
        "id": "qb6qihdSta5i",
        "colab_type": "code",
        "colab": {
          "base_uri": "https://localhost:8080/",
          "height": 35
        },
        "outputId": "9c29e6d6-1dc6-4b6f-b6e4-af36f7018bfb"
      },
      "cell_type": "code",
      "source": [
        "y_pred3=model3.predict_classes(X_test, verbose=1)"
      ],
      "execution_count": 0,
      "outputs": [
        {
          "output_type": "stream",
          "text": [
            "66292/66292 [==============================] - 32s 477us/step\n"
          ],
          "name": "stdout"
        }
      ]
    },
    {
      "metadata": {
        "id": "IxKn6SxSuQJ5",
        "colab_type": "code",
        "colab": {
          "base_uri": "https://localhost:8080/",
          "height": 204
        },
        "outputId": "16b247f7-3218-45e8-8dcf-93df3af45ff7"
      },
      "cell_type": "code",
      "source": [
        "sub.Sentiment=y_pred3\n",
        "sub.to_csv('./drive/My Drive/data/moives/sub3_CNN_GRU.csv',index=False)\n",
        "sub.head()"
      ],
      "execution_count": 0,
      "outputs": [
        {
          "output_type": "execute_result",
          "data": {
            "text/html": [
              "<div>\n",
              "<style scoped>\n",
              "    .dataframe tbody tr th:only-of-type {\n",
              "        vertical-align: middle;\n",
              "    }\n",
              "\n",
              "    .dataframe tbody tr th {\n",
              "        vertical-align: top;\n",
              "    }\n",
              "\n",
              "    .dataframe thead th {\n",
              "        text-align: right;\n",
              "    }\n",
              "</style>\n",
              "<table border=\"1\" class=\"dataframe\">\n",
              "  <thead>\n",
              "    <tr style=\"text-align: right;\">\n",
              "      <th></th>\n",
              "      <th>PhraseId</th>\n",
              "      <th>Sentiment</th>\n",
              "    </tr>\n",
              "  </thead>\n",
              "  <tbody>\n",
              "    <tr>\n",
              "      <th>0</th>\n",
              "      <td>156061</td>\n",
              "      <td>2</td>\n",
              "    </tr>\n",
              "    <tr>\n",
              "      <th>1</th>\n",
              "      <td>156062</td>\n",
              "      <td>2</td>\n",
              "    </tr>\n",
              "    <tr>\n",
              "      <th>2</th>\n",
              "      <td>156063</td>\n",
              "      <td>2</td>\n",
              "    </tr>\n",
              "    <tr>\n",
              "      <th>3</th>\n",
              "      <td>156064</td>\n",
              "      <td>2</td>\n",
              "    </tr>\n",
              "    <tr>\n",
              "      <th>4</th>\n",
              "      <td>156065</td>\n",
              "      <td>2</td>\n",
              "    </tr>\n",
              "  </tbody>\n",
              "</table>\n",
              "</div>"
            ],
            "text/plain": [
              "   PhraseId  Sentiment\n",
              "0  156061    2        \n",
              "1  156062    2        \n",
              "2  156063    2        \n",
              "3  156064    2        \n",
              "4  156065    2        "
            ]
          },
          "metadata": {
            "tags": []
          },
          "execution_count": 46
        }
      ]
    },
    {
      "metadata": {
        "id": "wWvB-Jijvy3k",
        "colab_type": "text"
      },
      "cell_type": "markdown",
      "source": [
        "##4. Bidirectional GRU##"
      ]
    },
    {
      "metadata": {
        "id": "6KxGss7-v8K0",
        "colab_type": "code",
        "colab": {
          "base_uri": "https://localhost:8080/",
          "height": 323
        },
        "outputId": "fcbe8904-4cf1-49af-e98b-84060d2e391e"
      },
      "cell_type": "code",
      "source": [
        "model4 = Sequential()\n",
        "\n",
        "model4.add(Embedding(max_features, 100, input_length=max_words))\n",
        "model4.add(SpatialDropout1D(0.25))\n",
        "model4.add(Bidirectional(GRU(128)))\n",
        "model4.add(Dropout(0.5))\n",
        "\n",
        "model4.add(Dense(5, activation='softmax'))\n",
        "model4.compile(loss='categorical_crossentropy', optimizer='adam', metrics=['accuracy'])\n",
        "model4.summary()"
      ],
      "execution_count": 0,
      "outputs": [
        {
          "output_type": "stream",
          "text": [
            "_________________________________________________________________\n",
            "Layer (type)                 Output Shape              Param #   \n",
            "=================================================================\n",
            "embedding_4 (Embedding)      (None, 48, 100)           1373200   \n",
            "_________________________________________________________________\n",
            "spatial_dropout1d_1 (Spatial (None, 48, 100)           0         \n",
            "_________________________________________________________________\n",
            "bidirectional_1 (Bidirection (None, 256)               175872    \n",
            "_________________________________________________________________\n",
            "dropout_6 (Dropout)          (None, 256)               0         \n",
            "_________________________________________________________________\n",
            "dense_6 (Dense)              (None, 5)                 1285      \n",
            "=================================================================\n",
            "Total params: 1,550,357\n",
            "Trainable params: 1,550,357\n",
            "Non-trainable params: 0\n",
            "_________________________________________________________________\n"
          ],
          "name": "stdout"
        }
      ]
    },
    {
      "metadata": {
        "id": "RXSHxHQlwBVb",
        "colab_type": "code",
        "colab": {
          "base_uri": "https://localhost:8080/",
          "height": 179
        },
        "outputId": "35e39c8d-4b5d-4bb9-8f82-0259ffdc5de3"
      },
      "cell_type": "code",
      "source": [
        "%%time\n",
        "history4=model4.fit(X_train, y_train, validation_data=(X_val, y_val),epochs=epochs, batch_size=batch_size, verbose=1)"
      ],
      "execution_count": 0,
      "outputs": [
        {
          "output_type": "stream",
          "text": [
            "Train on 124848 samples, validate on 31212 samples\n",
            "Epoch 1/3\n",
            "124848/124848 [==============================] - 202s 2ms/step - loss: 1.0023 - acc: 0.5960 - val_loss: 0.8527 - val_acc: 0.6498\n",
            "Epoch 2/3\n",
            "124848/124848 [==============================] - 200s 2ms/step - loss: 0.8033 - acc: 0.6691 - val_loss: 0.8081 - val_acc: 0.6715\n",
            "Epoch 3/3\n",
            "124848/124848 [==============================] - 201s 2ms/step - loss: 0.7342 - acc: 0.6957 - val_loss: 0.8075 - val_acc: 0.6731\n",
            "CPU times: user 11min 51s, sys: 1min 19s, total: 13min 10s\n",
            "Wall time: 10min 4s\n"
          ],
          "name": "stdout"
        }
      ]
    },
    {
      "metadata": {
        "id": "wHM5Tp_BwQtq",
        "colab_type": "code",
        "colab": {
          "base_uri": "https://localhost:8080/",
          "height": 35
        },
        "outputId": "1bf6ad5b-5269-4a30-b2e5-378771e9f1e0"
      },
      "cell_type": "code",
      "source": [
        "y_pred4=model4.predict_classes(X_test, verbose=1)"
      ],
      "execution_count": 0,
      "outputs": [
        {
          "output_type": "stream",
          "text": [
            "66292/66292 [==============================] - 107s 2ms/step\n"
          ],
          "name": "stdout"
        }
      ]
    },
    {
      "metadata": {
        "id": "yR48ZwR7zDLj",
        "colab_type": "code",
        "colab": {
          "base_uri": "https://localhost:8080/",
          "height": 204
        },
        "outputId": "04cf10d0-c843-4746-ddf2-9d07a7196612"
      },
      "cell_type": "code",
      "source": [
        "sub.Sentiment=y_pred4\n",
        "sub.to_csv('./drive/My Drive/data/moives/sub4_Bidirectional_GRU.csv',index=False)\n",
        "sub.head()"
      ],
      "execution_count": 0,
      "outputs": [
        {
          "output_type": "execute_result",
          "data": {
            "text/html": [
              "<div>\n",
              "<style scoped>\n",
              "    .dataframe tbody tr th:only-of-type {\n",
              "        vertical-align: middle;\n",
              "    }\n",
              "\n",
              "    .dataframe tbody tr th {\n",
              "        vertical-align: top;\n",
              "    }\n",
              "\n",
              "    .dataframe thead th {\n",
              "        text-align: right;\n",
              "    }\n",
              "</style>\n",
              "<table border=\"1\" class=\"dataframe\">\n",
              "  <thead>\n",
              "    <tr style=\"text-align: right;\">\n",
              "      <th></th>\n",
              "      <th>PhraseId</th>\n",
              "      <th>Sentiment</th>\n",
              "    </tr>\n",
              "  </thead>\n",
              "  <tbody>\n",
              "    <tr>\n",
              "      <th>0</th>\n",
              "      <td>156061</td>\n",
              "      <td>2</td>\n",
              "    </tr>\n",
              "    <tr>\n",
              "      <th>1</th>\n",
              "      <td>156062</td>\n",
              "      <td>2</td>\n",
              "    </tr>\n",
              "    <tr>\n",
              "      <th>2</th>\n",
              "      <td>156063</td>\n",
              "      <td>2</td>\n",
              "    </tr>\n",
              "    <tr>\n",
              "      <th>3</th>\n",
              "      <td>156064</td>\n",
              "      <td>2</td>\n",
              "    </tr>\n",
              "    <tr>\n",
              "      <th>4</th>\n",
              "      <td>156065</td>\n",
              "      <td>2</td>\n",
              "    </tr>\n",
              "  </tbody>\n",
              "</table>\n",
              "</div>"
            ],
            "text/plain": [
              "   PhraseId  Sentiment\n",
              "0  156061    2        \n",
              "1  156062    2        \n",
              "2  156063    2        \n",
              "3  156064    2        \n",
              "4  156065    2        "
            ]
          },
          "metadata": {
            "tags": []
          },
          "execution_count": 50
        }
      ]
    },
    {
      "metadata": {
        "id": "1j-gB7Fz0Coi",
        "colab_type": "text"
      },
      "cell_type": "markdown",
      "source": [
        "##5. Glove word embedding##"
      ]
    },
    {
      "metadata": {
        "id": "KGIeLkEPz6rz",
        "colab_type": "code",
        "colab": {}
      },
      "cell_type": "code",
      "source": [
        "def get_coefs(word, *arr):\n",
        "    return word, np.asarray(arr, dtype='float32')\n",
        "    \n",
        "def get_embed_mat(EMBEDDING_FILE, max_features,embed_dim):\n",
        "    # word vectors\n",
        "    embeddings_index = dict(get_coefs(*o.rstrip().rsplit(' ')) for o in open(EMBEDDING_FILE, encoding='utf8'))\n",
        "    print('Found %s word vectors.' % len(embeddings_index))\n",
        "\n",
        "    # embedding matrix\n",
        "    word_index = tokenizer.word_index\n",
        "    num_words = min(max_features, len(word_index) + 1)\n",
        "    all_embs = np.stack(embeddings_index.values()) #for random init\n",
        "    embedding_matrix = np.random.normal(all_embs.mean(), all_embs.std(), \n",
        "                                        (num_words, embed_dim))\n",
        "    for word, i in word_index.items():\n",
        "        if i >= max_features:\n",
        "            continue\n",
        "        embedding_vector = embeddings_index.get(word)\n",
        "        if embedding_vector is not None:\n",
        "            embedding_matrix[i] = embedding_vector\n",
        "    max_features = embedding_matrix.shape[0]\n",
        "    \n",
        "    return embedding_matrix"
      ],
      "execution_count": 0,
      "outputs": []
    },
    {
      "metadata": {
        "id": "SqXfId920H50",
        "colab_type": "code",
        "colab": {
          "base_uri": "https://localhost:8080/",
          "height": 53
        },
        "outputId": "07fccf8c-f0db-42e1-a52e-71929fae21a6"
      },
      "cell_type": "code",
      "source": [
        "# embedding matrix\n",
        "EMBEDDING_FILE = './drive/My Drive/data/glove.6B.100d.txt'\n",
        "embed_dim = 100 #word vector dim\n",
        "embedding_matrix = get_embed_mat(EMBEDDING_FILE,max_features,embed_dim)\n",
        "print(embedding_matrix.shape)"
      ],
      "execution_count": 0,
      "outputs": [
        {
          "output_type": "stream",
          "text": [
            "Found 400000 word vectors.\n",
            "(13732, 100)\n"
          ],
          "name": "stdout"
        }
      ]
    },
    {
      "metadata": {
        "id": "Oi_A13q-45Vt",
        "colab_type": "code",
        "colab": {
          "base_uri": "https://localhost:8080/",
          "height": 359
        },
        "outputId": "f2cfc4bf-c604-43bf-9de9-54bef38ca1a7"
      },
      "cell_type": "code",
      "source": [
        "model5 = Sequential()\n",
        "model5.add(Embedding(max_features, embed_dim, input_length=X_train.shape[1],weights=[embedding_matrix],trainable=True))\n",
        "model5.add(SpatialDropout1D(0.25))\n",
        "model5.add(Bidirectional(GRU(128,return_sequences=True)))\n",
        "model5.add(Bidirectional(GRU(64,return_sequences=False)))\n",
        "model5.add(Dropout(0.5))\n",
        "model5.add(Dense(num_classes, activation='softmax'))\n",
        "model5.compile(loss='categorical_crossentropy', optimizer='adam', metrics=['accuracy'])\n",
        "model5.summary()"
      ],
      "execution_count": 0,
      "outputs": [
        {
          "output_type": "stream",
          "text": [
            "_________________________________________________________________\n",
            "Layer (type)                 Output Shape              Param #   \n",
            "=================================================================\n",
            "embedding_5 (Embedding)      (None, 48, 100)           1373200   \n",
            "_________________________________________________________________\n",
            "spatial_dropout1d_2 (Spatial (None, 48, 100)           0         \n",
            "_________________________________________________________________\n",
            "bidirectional_2 (Bidirection (None, 48, 256)           175872    \n",
            "_________________________________________________________________\n",
            "bidirectional_3 (Bidirection (None, 128)               123264    \n",
            "_________________________________________________________________\n",
            "dropout_7 (Dropout)          (None, 128)               0         \n",
            "_________________________________________________________________\n",
            "dense_7 (Dense)              (None, 5)                 645       \n",
            "=================================================================\n",
            "Total params: 1,672,981\n",
            "Trainable params: 1,672,981\n",
            "Non-trainable params: 0\n",
            "_________________________________________________________________\n"
          ],
          "name": "stdout"
        }
      ]
    },
    {
      "metadata": {
        "id": "A69fKi3G5DXU",
        "colab_type": "code",
        "colab": {
          "base_uri": "https://localhost:8080/",
          "height": 215
        },
        "outputId": "42cad092-9f4b-4c6b-98e7-b61ba78bec7a"
      },
      "cell_type": "code",
      "source": [
        "%%time\n",
        "history5=model5.fit(X_train, y_train, validation_data=(X_val, y_val),epochs=4, batch_size=batch_size, verbose=1)"
      ],
      "execution_count": 0,
      "outputs": [
        {
          "output_type": "stream",
          "text": [
            "Train on 124848 samples, validate on 31212 samples\n",
            "Epoch 1/4\n",
            "124848/124848 [==============================] - 389s 3ms/step - loss: 0.9966 - acc: 0.5914 - val_loss: 0.8396 - val_acc: 0.6542\n",
            "Epoch 2/4\n",
            "124848/124848 [==============================] - 387s 3ms/step - loss: 0.8483 - acc: 0.6497 - val_loss: 0.7916 - val_acc: 0.6759\n",
            "Epoch 3/4\n",
            "124848/124848 [==============================] - 385s 3ms/step - loss: 0.7870 - acc: 0.6724 - val_loss: 0.7744 - val_acc: 0.6793\n",
            "Epoch 4/4\n",
            "124848/124848 [==============================] - 384s 3ms/step - loss: 0.7450 - acc: 0.6921 - val_loss: 0.7648 - val_acc: 0.6822\n",
            "CPU times: user 30min 1s, sys: 3min 6s, total: 33min 7s\n",
            "Wall time: 25min 49s\n"
          ],
          "name": "stdout"
        }
      ]
    },
    {
      "metadata": {
        "id": "lBsk1oL1iuKb",
        "colab_type": "code",
        "colab": {
          "base_uri": "https://localhost:8080/",
          "height": 35
        },
        "outputId": "9499adc7-bf39-471e-8734-bf82905cb9ec"
      },
      "cell_type": "code",
      "source": [
        "y_pred5=model5.predict_classes(X_test, verbose=1)"
      ],
      "execution_count": 0,
      "outputs": [
        {
          "output_type": "stream",
          "text": [
            "66292/66292 [==============================] - 203s 3ms/step\n"
          ],
          "name": "stdout"
        }
      ]
    },
    {
      "metadata": {
        "id": "a3Q03kHmjBg0",
        "colab_type": "code",
        "colab": {
          "base_uri": "https://localhost:8080/",
          "height": 204
        },
        "outputId": "9c7838ce-19f9-489c-efc3-876ef52e32fd"
      },
      "cell_type": "code",
      "source": [
        "sub.Sentiment=y_pred5\n",
        "sub.to_csv('./drive/My Drive/data/moives/sub5_glove.csv',index=False)\n",
        "sub.head()"
      ],
      "execution_count": 0,
      "outputs": [
        {
          "output_type": "execute_result",
          "data": {
            "text/html": [
              "<div>\n",
              "<style scoped>\n",
              "    .dataframe tbody tr th:only-of-type {\n",
              "        vertical-align: middle;\n",
              "    }\n",
              "\n",
              "    .dataframe tbody tr th {\n",
              "        vertical-align: top;\n",
              "    }\n",
              "\n",
              "    .dataframe thead th {\n",
              "        text-align: right;\n",
              "    }\n",
              "</style>\n",
              "<table border=\"1\" class=\"dataframe\">\n",
              "  <thead>\n",
              "    <tr style=\"text-align: right;\">\n",
              "      <th></th>\n",
              "      <th>PhraseId</th>\n",
              "      <th>Sentiment</th>\n",
              "    </tr>\n",
              "  </thead>\n",
              "  <tbody>\n",
              "    <tr>\n",
              "      <th>0</th>\n",
              "      <td>156061</td>\n",
              "      <td>2</td>\n",
              "    </tr>\n",
              "    <tr>\n",
              "      <th>1</th>\n",
              "      <td>156062</td>\n",
              "      <td>2</td>\n",
              "    </tr>\n",
              "    <tr>\n",
              "      <th>2</th>\n",
              "      <td>156063</td>\n",
              "      <td>2</td>\n",
              "    </tr>\n",
              "    <tr>\n",
              "      <th>3</th>\n",
              "      <td>156064</td>\n",
              "      <td>2</td>\n",
              "    </tr>\n",
              "    <tr>\n",
              "      <th>4</th>\n",
              "      <td>156065</td>\n",
              "      <td>2</td>\n",
              "    </tr>\n",
              "  </tbody>\n",
              "</table>\n",
              "</div>"
            ],
            "text/plain": [
              "   PhraseId  Sentiment\n",
              "0  156061    2        \n",
              "1  156062    2        \n",
              "2  156063    2        \n",
              "3  156064    2        \n",
              "4  156065    2        "
            ]
          },
          "metadata": {
            "tags": []
          },
          "execution_count": 56
        }
      ]
    },
    {
      "metadata": {
        "id": "ahqNRMZeNcit",
        "colab_type": "text"
      },
      "cell_type": "markdown",
      "source": [
        "##combine all output##"
      ]
    },
    {
      "metadata": {
        "id": "7960a-DB5Ips",
        "colab_type": "code",
        "colab": {}
      },
      "cell_type": "code",
      "source": [
        "sub_all=pd.DataFrame({'model1':y_pred1,'model2':y_pred2,'model3':y_pred3,'model4':y_pred4,'model5':y_pred5})\n",
        "pred_mode=sub_all.agg('mode',axis=1)[0].values\n",
        "sub_all.head()"
      ],
      "execution_count": 0,
      "outputs": []
    },
    {
      "metadata": {
        "id": "whSmbDOJQkLE",
        "colab_type": "code",
        "colab": {}
      },
      "cell_type": "code",
      "source": [
        "pred_mode=[int(i) for i in pred_mode]"
      ],
      "execution_count": 0,
      "outputs": []
    },
    {
      "metadata": {
        "id": "iFRPYcp0QnoP",
        "colab_type": "code",
        "colab": {
          "base_uri": "https://localhost:8080/",
          "height": 204
        },
        "outputId": "f8a24223-9b41-4fad-e22d-08062f00e2d7"
      },
      "cell_type": "code",
      "source": [
        "sub.Sentiment=pred_mode\n",
        "sub.to_csv('./drive/My Drive/data/moives/sub_mode.csv',index=False)\n",
        "sub.head()"
      ],
      "execution_count": 0,
      "outputs": [
        {
          "output_type": "execute_result",
          "data": {
            "text/html": [
              "<div>\n",
              "<style scoped>\n",
              "    .dataframe tbody tr th:only-of-type {\n",
              "        vertical-align: middle;\n",
              "    }\n",
              "\n",
              "    .dataframe tbody tr th {\n",
              "        vertical-align: top;\n",
              "    }\n",
              "\n",
              "    .dataframe thead th {\n",
              "        text-align: right;\n",
              "    }\n",
              "</style>\n",
              "<table border=\"1\" class=\"dataframe\">\n",
              "  <thead>\n",
              "    <tr style=\"text-align: right;\">\n",
              "      <th></th>\n",
              "      <th>PhraseId</th>\n",
              "      <th>Sentiment</th>\n",
              "    </tr>\n",
              "  </thead>\n",
              "  <tbody>\n",
              "    <tr>\n",
              "      <th>0</th>\n",
              "      <td>156061</td>\n",
              "      <td>2</td>\n",
              "    </tr>\n",
              "    <tr>\n",
              "      <th>1</th>\n",
              "      <td>156062</td>\n",
              "      <td>2</td>\n",
              "    </tr>\n",
              "    <tr>\n",
              "      <th>2</th>\n",
              "      <td>156063</td>\n",
              "      <td>2</td>\n",
              "    </tr>\n",
              "    <tr>\n",
              "      <th>3</th>\n",
              "      <td>156064</td>\n",
              "      <td>2</td>\n",
              "    </tr>\n",
              "    <tr>\n",
              "      <th>4</th>\n",
              "      <td>156065</td>\n",
              "      <td>2</td>\n",
              "    </tr>\n",
              "  </tbody>\n",
              "</table>\n",
              "</div>"
            ],
            "text/plain": [
              "   PhraseId  Sentiment\n",
              "0  156061    2        \n",
              "1  156062    2        \n",
              "2  156063    2        \n",
              "3  156064    2        \n",
              "4  156065    2        "
            ]
          },
          "metadata": {
            "tags": []
          },
          "execution_count": 59
        }
      ]
    },
    {
      "metadata": {
        "id": "GukmDh8CkFAf",
        "colab_type": "code",
        "colab": {}
      },
      "cell_type": "code",
      "source": [
        ""
      ],
      "execution_count": 0,
      "outputs": []
    }
  ]
}